package com.hainatong.shiphelper.module.holiday;


import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentListViewBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.adapter.SubFunctionAdapter;
import com.hainatong.shiphelper.mainhome.entity.FunctionEntity;
import com.hainatong.shiphelper.module.holiday.entity.DictFunBean;
import com.hainatong.shiphelper.module.holiday.entity.HolidayTypeBean;
import com.hainatong.shiphelper.module.holiday.entity.VacationParam;
import com.hainatong.shiphelper.module.holiday.fragment.NormalHolidayFragment;
import com.hainatong.shiphelper.module.holiday.viewmodel.VacationViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;


import java.util.ArrayList;
import java.util.List;

public class HolidayMgrFragment extends MvvmLazyFragment<FragmentListViewBinding, VacationViewModel>
{
    private SubFunctionAdapter adapter;

    private List<DictFunBean> vacationTypeList;

    private List<FunctionEntity> funList = new ArrayList<>();
    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_list_view;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
        adapter = new SubFunctionAdapter();
        viewDataBinding.recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        viewDataBinding.recyclerview.setHasFixedSize(true);
        viewDataBinding.recyclerview.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter1, view1, position) -> {

            DictFunBean tempFun = vacationTypeList.get(position);
            HolidayTypeBean type;
            switch ( Integer.parseInt( adapter.getItem(position).getContentTag() ) )
            {
                // 正常休假
                case Constants.FUN_ZHENGCHANGXIUJIA:

                    type = new HolidayTypeBean(NormalHolidayFragment.normalHolidayType,tempFun.getDict_value(),tempFun.getUuid() );
                    ComFragmentActivity.startViewByParam(getActivity(), Constants.VIEW_NORMAL_HOLIDAY , GsonUtil.obj2json( type ));

                    break;
                case Constants.FUN_LINSHIXIUJIA:
                    type = new HolidayTypeBean(NormalHolidayFragment.tempHolidayType,tempFun.getDict_value(),tempFun.getUuid() );
                    ComFragmentActivity.startViewByParam(getActivity(), Constants.VIEW_NORMAL_HOLIDAY , GsonUtil.obj2json( type ));

                    break;
                case Constants.FUN_YANQIXIUJIA:
                    type = new HolidayTypeBean(NormalHolidayFragment.delayHolidayType,tempFun.getDict_value(),tempFun.getUuid() );
                    ComFragmentActivity.startViewByParam(getActivity(), Constants.VIEW_NORMAL_HOLIDAY , GsonUtil.obj2json( type ));

                    break;
                case Constants.FUN_GENGHUANXIUJIA:
                    type = new HolidayTypeBean(NormalHolidayFragment.changeHolidayType,tempFun.getDict_value(),tempFun.getUuid() );
                    ComFragmentActivity.startViewByParam(getActivity(), Constants.VIEW_NORMAL_HOLIDAY , GsonUtil.obj2json( type ));

                    break;
                case Constants.FUN_NOTWORK:
                    // 不能试岗 只有船长操作  直接把 提交参数 传过去
                    VacationParam vacParam = new VacationParam();
                    // 设置 申请休假 类型  不能试岗
                    vacParam.setVactiontype(tempFun.getDict_value());
                    //"a6e91f09-5d1c-4e04-b2c7-0ad39e1c7e9a"
                    vacParam.setVactiontype_id(tempFun.getUuid());

                    ComFragmentActivity.startViewByParam( getActivity(),Constants.VIEW_NO_JOB, GsonUtil.obj2json( vacParam ));
                    break;
            }
            //ToastUtils.showShort( adapter.getItem(position).getTitle() );

        });
    }


    @Override
    protected void lazyLoad()
    {
        //查 休假类型
        viewModel.queryVacationTypeList();
        // 查到 休假 类型 后 赛选
        viewModel.getVacationTypeResult().observe( this , result -> {
            if( result.isSuccess() )
            {
                if( result.getData() != null && !result.getData().isEmpty() )
                {
                    vacationTypeList = result.getData();
                    //根据  登录用户角色显示  船长 显示 不能 适岗

                    for(int i = 0;i < vacationTypeList.size();i++ )
                    {

                        DictFunBean typeBean = vacationTypeList.get(i);

                        if( typeBean.getDict_value().contains("正常休假") )
                        {
                            //getString(R.string.zhengchang_xiujia)
                            funList.add(new FunctionEntity(typeBean.getDict_value(),""+Constants.FUN_ZHENGCHANGXIUJIA,R.mipmap.ic_holiday));
                        }
                        else if(typeBean.getDict_value().contains("临时休假") && !typeBean.getDict_value().contains("不能适岗") )
                        {
                            // getString(R.string.linshi_xiujia)
                            funList.add(new FunctionEntity(typeBean.getDict_value(),""+Constants.FUN_LINSHIXIUJIA,R.mipmap.ic_temp_holiday));
                        }
                        else if( typeBean.getDict_value().contains("延期休假") )
                        {
                            //getString(R.string.yanqi_xiujia)
                            funList.add(new FunctionEntity(typeBean.getDict_value(),""+Constants.FUN_YANQIXIUJIA,R.mipmap.ic_delay_holiday));
                        }
                        else if( typeBean.getDict_value().contains("更换证书") )
                        {
                            //getString(R.string.zhengshu_xiujia)
                            funList.add(new FunctionEntity(typeBean.getDict_value(),""+Constants.FUN_GENGHUANXIUJIA,R.mipmap.ic_certificate_holiday));
                        }
                        else if( typeBean.getDict_value().contains("不能适岗") )
                        {
                            //根据  登录用户角色显示  船长 显示 不能 适岗

                            //if(GlobalConfig.getInstance().getUserInfo().getRoleName().contains("船长"))
                            if(true)
                            {
                                funList.add(new FunctionEntity(getString(R.string.can_not_job),""+Constants.FUN_NOTWORK,R.mipmap.ic_resign));
                            }
                            else
                            {
                                vacationTypeList.remove( typeBean );
                            }
                        }
                    }
                    adapter.setList( funList );
                }
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        } );


    }
}
